<template>
    <!-- 查看详情 -->
    <el-dialog 
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.table.extend!.infoId ? true:false"
        @close="baTable.table.extend!.infoId = 0">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">{{ t('Info') }}</div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'">
                <el-descriptions :column="2" border>
                    <el-descriptions-item label="审核状态">
                        {{ getStatusText(baTable.form.extend!.info.status) }}
                    </el-descriptions-item>
                    <el-descriptions-item label="提交时间">
                        {{ timeFormat(baTable.form.extend!.info.create_time) }}
                    </el-descriptions-item>
                    <el-descriptions-item :width="120" :span="2" label="案件名称">
                        {{ baTable.form.extend!.info.wo.title }}
                    </el-descriptions-item>
                    <el-descriptions-item :width="120" :span="2" label="报告文件">
                        <el-button type="primary" size="small" v-if="baTable.form.extend!.info.file" @click="onDownload(baTable.form.extend!.info.file)">
                            点击下载 {{ getFileSuffix(baTable.form.extend!.info.file) }} 文件
                        </el-button>
                    </el-descriptions-item>
                    <el-descriptions-item :width="140" :span="2" label="案件下一步状态">
                        {{ baTable.form.extend!.info.process_text }}
                    </el-descriptions-item>
                    <el-descriptions-item :width="120" :span="2" label="初步分析结果">
                        {{ baTable.form.extend!.info.remark }}
                    </el-descriptions-item>

                </el-descriptions>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - 20px)'">
                <el-button @click="baTable.table.extend!.infoId = 0">{{ t('Cancel') }}</el-button>
                <el-button v-if="baTable.form.extend!.info.status === 0" v-blur :loading="baTable.form.loading" @click="onSubmitCheck()" type="primary">
                    审核
                </el-button>
                <el-button v-else v-blur type="primary" disabled>
                    已审核
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { timeFormat, fullUrl } from '/@/utils/common'
import { checkReport } from '/@/api/backend/workorder'

const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const onDownload = (url: string) => {
    if (url) {
        const a = document.createElement('a')
        a.href = fullUrl(url)
        a.download = ''
        a.target = '_blank'
        a.click()
    }
}

const getFileSuffix = (filePath: string) => {
    if (filePath) {
        const arr = filePath.split('.')
        return arr[arr.length - 1]
    }
    return '未知'
}

const getStatusText = (status: number) => {
    // 0待发送，1已接收，2进行中，3挂起，4结束，5取消
    switch (status) {
        case 0:
            return '待审核'
        case 1:
            return '审核通过'
        case 2:
            return '已生成'
        case 3:
            return '已发送'
        case 4:
            return '驳回'
        default:
            return '未知'
    }
}

const getStateText = (status: string) => {
    // 0待发送，1已接收，2进行中，3挂起，4结束，5取消
    switch (status) {
        case 'initial':
            return '审核通过'
        case 'detailed':
            return '已生成'
        case 'support':
            return '已发送'
        case 'end':
            return '驳回'
        default:
            return '未知'
    }
}

const onSubmitCheck = () => {
    // state.dialog.loading = true
    checkReport({
        id: baTable.form.extend!.info.id,
        ispass: 1
    })
        .then(() => {
            //baTable.toggleForm
            baTable.getIndex()
        })
        .finally(() => {
            // state.dialog.loading = false
            baTable.table.extend!.infoId = 0
        })
}
</script>

<style scoped lang="scss">
.table-el-tree {
    :deep(.el-tree-node) {
        white-space: unset;
    }
    :deep(.el-tree-node__content) {
        display: block;
        align-items: unset;
        height: unset;
    }
}
</style>
